<?php

namespace App\Models;

use App\Scopes\OrderScope;
use Illuminate\Database\Eloquent\Model;

class Order extends Model
{
    const REFUND_STATUS_PENDING = 'pending';
    const REFUND_STATUS_APPLIED = 'applied';
    const REFUND_STATUS_PROCESSING = 'processing';
    const REFUND_STATUS_SUCCESS = 'success';
    const REFUND_STATUS_FAILED = 'failed';

    const MAKE_STATUS_PROCESSING = 'processing';
    const MAKE_STATUS_SUCCESS = 'received';

    public static $refundStatusMap = [
        self::REFUND_STATUS_PENDING    => '未退款',
        self::REFUND_STATUS_APPLIED    => '已申请退款',
        self::REFUND_STATUS_PROCESSING => '退款中',
        self::REFUND_STATUS_SUCCESS    => '退款成功',
        self::REFUND_STATUS_FAILED     => '退款失败',
    ];

    public static $makeStatusMap = [
        self::MAKE_STATUS_PROCESSING => '制作中',
        self::MAKE_STATUS_SUCCESS  => '制作完成',
    ];

    protected $fillable = [
        'out_trade_no',
        'transaction_id',
        'body',
        'detail',
        'total_fee',
        'paid_at',
        'coupon_id',
        'user_id',
        'product_id',
        'status',
        'refund_status',
        'refund_no',
        'closed',
        'extra'
    ];

    protected $casts = [
        'closed'    => 'boolean',
        'extra'     => 'json',
    ];

    protected $dates = [
        'paid_at',
    ];

    protected static function boot()
    {
        parent::boot();

        static::addGlobalScope(new OrderScope());
    }

    public function user()
    {
        return $this->belongsTo(User::class, 'user_id');
    }

    public function product()
    {
        return $this->belongsTo(Product::class, 'product_id');
    }

}
